package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionVO;
import com.yonyou.pmclouds.safeinspection.rmiitf.SafeinspectionQueryService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐老板
 * @date 2020/4/30   10:02
 */
@Component
public class SafeinspectionMessageConstructor extends AbstractMessageNoticeConstructor {

    private final SafeinspectionQueryService queryService;
    private final ProjectSimpleQuery simpleQuery;

    @Autowired
    public SafeinspectionMessageConstructor(SafeinspectionQueryService queryService, ProjectSimpleQuery simpleQuery) {
        this.queryService = queryService;
        this.simpleQuery = simpleQuery;
    }

    //推送通知的启动方法
    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        MessageNoticeVO messageNoticeVO = coverBusinessVOToMessageVO(vo);
        if (messageNoticeVO==null){
            return new MessageNoticeVO[0];
        }


        SafeInspectionVO inspectionVO = queryService.queryByOne(messageNoticeVO.getRelateKey());
        ProjectVO projectVO = simpleQuery.queryProject(inspectionVO.getPk_project());
        String prefix = "";
        if (inspectionVO.getBillstatus()== BillStatusConst.COMMON_APPROVED_STATUS){
            prefix=APPROVAL_SUFFIX_ADOPT_MESSAGE;
        }else if(inspectionVO.getBillstatus()==BillStatusConst.COMMON_REJECT_STATUS){
            prefix=APPROVAL_SUFFIX_REJECT_MESSAGE;
        }
        //拼接通知内容
        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" + projectVO.getSname() + "]的安全巡查记录" + prefix;
        //给制单人发送消息
        List<MessageNoticeVO> noticeVOS =new ArrayList<>();
        noticeVOS.add(getNoticeVO(inspectionVO,projectVO.getSname(),messageContent,inspectionVO.getCreator()));

        //接下来给抄送人发送信息
        List<CopyPersonVO> copyPersonVOS = inspectionVO.getCopyPersonVOS();
        if (CollectionUtils.isNotEmpty(copyPersonVOS)){
            for (CopyPersonVO copyPersonVO : copyPersonVOS) {
                noticeVOS.add(getNoticeVO(inspectionVO,projectVO.getSname(),messageContent,copyPersonVO.getCopyPerson()));
            }
        }


        return noticeVOS.toArray(new MessageNoticeVO[0]);
    }

    //生成消息通知类 表实体类 项目名称 内容 创建人  既给抄送人 也给制单人发送信息
    private MessageNoticeVO getNoticeVO(SafeInspectionVO inspectionVO, String sname, String messageContent, String person) {
        MessageNoticeVO messageNoticeVO =new MessageNoticeVO();
//        messageNoticeVO.setCreator(creator);  所以这里设置创建人没有意义
        messageNoticeVO.setMessageTitle(sname);
        messageNoticeVO.setMessageContent(messageContent);
        messageNoticeVO.setMessageReceiver(person);

        messageNoticeVO.setRelateType(inspectionVO.getBill_type());
        messageNoticeVO.setRelateKey(inspectionVO.getPk_safeinspection());
        messageNoticeVO.setPkTenant(inspectionVO.getPk_tenant());

        return messageNoticeVO;
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.SAFEINSPECTION_BILLTYPE;
    }

    @Override
    public MessageNoticeVO[] generateNotice(Object vo) throws BusinessException {
        return new MessageNoticeVO[0];
    }
}
